package org.tjc.net.manager.config.json;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;


/**
 * @Author: tjc
 * @description
 * @Date Created in 11:09 2020-12-26
 */
@Data
public class MetaNgCommand {
    private String name;
    //配置类型 0command   1 module
    private int type;

    private Class clazz;

    //是否需要显式配置
    private boolean require;

    //可出现在的上下文
    private int context;


    private ModuleSet mSet;

    private CmdSet cSet;


    public static int CMD_LINE = 0;
    public static int CMD_MODULE = 1;

    public MetaNgCommand(String name, int type, boolean require, int context) {
        this.name = name;
        this.type = type;
        this.require = require;
        this.context = context;
    }


    public MetaNgCommand(String name, int type, boolean require, int context, CmdSet set) {
        this.name = name;
        this.type = type;
        this.require = require;
        this.context = context;
        this.cSet = set;
    }

    public MetaNgCommand(String name, int type, boolean require, int context, ModuleSet set) {
        this.name = name;
        this.type = type;
        this.require = require;
        this.context = context;
        this.mSet = set;
    }


    private static Map<String, MetaNgCommand> metaCmdMap = new HashMap<>();

    static {

        metaCmdMap.put(ConfConstant.HTTP, new MetaNgCommand(ConfConstant.HTTP, CMD_MODULE, true, 1, NgHttpModule.ngSet));
//        metaCmdMap.put(ConfConstant.EVENT, new MetaNgCommand(ConfConstant.EVENT, CMD_MODULE, true, 1, NgHttpModule.ngSet));
        metaCmdMap.put(ConfConstant.SERVER, new MetaNgCommand(ConfConstant.SERVER, CMD_MODULE, true, 1, NgServerModule.ngSet));
        metaCmdMap.put(ConfConstant.LOCATION, new MetaNgCommand(ConfConstant.LOCATION, CMD_MODULE, true, 1, NgLocationModule.ngSet));

        metaCmdMap.put(ConfConstant.SERVER_NAME, new MetaNgCommand(ConfConstant.SERVER_NAME, CMD_LINE, true, 1, NgCommand.ngSet));
        metaCmdMap.put(ConfConstant.port, new MetaNgCommand(ConfConstant.port, CMD_LINE, true, 1, NgCommand.ngSet));
        metaCmdMap.put(ConfConstant.LOCATION_PROXYPASS, new MetaNgCommand(ConfConstant.LOCATION_PROXYPASS, CMD_LINE, true, 1, NgCommand.ngSet));
        metaCmdMap.put(ConfConstant.PATH, new MetaNgCommand(ConfConstant.PATH, CMD_LINE, true, 1, NgCommand.ngSet));

    }

    public void invoke(NgModule ctx,String key, Object value) {
        this.cSet.invoke(ctx,key, value);
    }

    public void invoke(NgModule ctx, Configuration conf) {
        this.mSet.invoke(ctx, conf);
    }

    public static boolean contains(String key) {
        return metaCmdMap.containsKey(key);
    }

    public static MetaNgCommand get(String key) {
        return metaCmdMap.get(key);
    }

    public boolean isModule() {
        return type == CMD_MODULE;
    }

    public boolean isCmd() {
        return type == CMD_LINE;
    }
}
